package com.sskj.mine.ui.activity;


import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.sskj.common.base.App;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.component.DaggerUserDataComponent;
import com.sskj.mine.presenter.SetPayPwdActivityPresenter;
import com.sskj.mine.util.HideUtil;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import butterknife.BindView;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subscribers.DisposableSubscriber;

@Route(path = RConfig.MINE_SET_PAY_PWD)//设置支付密码
public class SetPayPwdActivity extends BaseActivity<SetPayPwdActivityPresenter> {
    @BindView(R2.id.etPayPwd)
    EditText etPayPwd;
    @BindView(R2.id.etPayPwdAgain)
    EditText etPayPwdAgain;
    @BindView(R2.id.tvPhone)
    TextView tvPhone;
    @BindView(R2.id.etCode)
    EditText etCode;
    @BindView(R2.id.tvGetCode)
    TextView tvGetCode;
    @BindView(R2.id.btSubmit)
    Button btSubmit;
    @Inject
    UserViewModel userViewModel;
    @BindView(R2.id.ivTogglePwd)
    ImageView ivTogglePwd;
    @BindView(R2.id.ivTogglePwdAgain)
    ImageView ivTogglePwdAgain;
    private UserData userData;
    private String successCode;
    private DisposableSubscriber<Long> disposableSubscriber;
    private boolean isSee;
    private boolean isSeeAgain;

    @Override
    protected int getLayoutResId() {
        return R.layout.mine_activity_set_pay_pwd;
    }

    @Override
    public SetPayPwdActivityPresenter getPresenter() {
        return new SetPayPwdActivityPresenter();
    }

    @Override
    protected void initView() {
        DaggerUserDataComponent.create().inject(this);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
            }
            updateUI();
        });

        ClickUtil.click(200, ivTogglePwdAgain, () -> {
            if (isSeeAgain) {
                isSeeAgain = false;
                etPayPwdAgain.setTransformationMethod(PasswordTransformationMethod.getInstance());
                ivTogglePwdAgain.setImageResource(R.mipmap.mine_icon_unsee);
            } else {
                isSeeAgain = true;
                etPayPwdAgain.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                ivTogglePwdAgain.setImageResource(R.mipmap.mine_icon_see);
            }
        });
        ClickUtil.click(200, ivTogglePwd, () -> {
            if (isSee) {
                isSee = false;
                etPayPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                ivTogglePwd.setImageResource(R.mipmap.mine_icon_unsee);
            } else {
                isSee = true;
                etPayPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                ivTogglePwd.setImageResource(R.mipmap.mine_icon_see);
            }
        });

        ClickUtil.click(tvGetCode, () -> { //获取验证码
            String pwd1 = etPayPwd.getText().toString().trim();
            String pwd2 = etPayPwdAgain.getText().toString().trim();
            if (TextUtils.isEmpty(pwd1)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mineSetPayPwdActivity1));
                return;
            } else if (TextUtils.isEmpty(pwd2)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mineSetPayPwdActivity2));
                return;
            }
            if (!pwd1.equals(pwd2)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mineSetPayPwdActivity3));
                return;
            }
            if (!CheckUtil.isPayPwd(pwd1)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mineSetPayPwdActivity4));
                return;
            }
            if (userData.getWay().equals("mobile")) {
                mPresenter.sendCode(userData.getMobile());
            } else {
                mPresenter.sendEmailCode(userData.getMail());
            }
        });
        ClickUtil.click(btSubmit, () -> { //提交修改
            String pwd1 = etPayPwd.getText().toString().trim();
            String pwd2 = etPayPwdAgain.getText().toString().trim();
            successCode = etCode.getText().toString().trim();
            if (TextUtils.isEmpty(pwd1)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mineSetPayPwdActivity1));
                return;
            } else if (TextUtils.isEmpty(pwd2)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mineSetPayPwdActivity2));
                return;
            }
            if (!pwd1.equals(pwd2)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mineSetPayPwdActivity3));
                return;
            }
            if (TextUtils.isEmpty(successCode)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mineSetPayPwdActivity8));
                return;
            }
            mPresenter.setPayPwd(successCode, pwd1, userData.getWay().equals("mobile") ? userData.getMobile() : userData.getMail());
        });
    }

    private void updateUI() {
        if (userData.getWay().equals("mobile")) {
            tvPhone.setText(String.format(App.INSTANCE.getString(R.string.mineSetPayPwdActivity9), HideUtil.getPhoneHide(userData.getMobile())));
        } else {
            tvPhone.setText(String.format(App.INSTANCE.getString(R.string.mineSetPayPwdActivity99), userData.getMail()));
        }
        if (TextUtils.isEmpty(userData.getTpwd())) {
            setTitle(App.INSTANCE.getString(R.string.mineSetPayPwdActivity0));
        } else {
            setTitle(App.INSTANCE.getString(R.string.mineSetPayPwdActivity01));
        }
    }

    public void onSmsResponseSuccess() {
        startCount();
    }

    private void startCount() {
        tvGetCode.setEnabled(false);
        disposableSubscriber = new DisposableSubscriber<Long>() {
            @Override
            public void onNext(Long aLong) {
                int time = (int) (60 - aLong);
                tvGetCode.setText(time + App.INSTANCE.getString(R.string.mineSetPayPwdActivity10));
            }

            @Override
            public void onError(Throwable t) {
                System.out.println(t.toString());
            }

            @Override
            public void onComplete() {
                tvGetCode.setText(App.INSTANCE.getString(R.string.mineSetPayPwdActivity11));
                tvGetCode.setEnabled(true);
                if (!disposableSubscriber.isDisposed()) {
                    disposableSubscriber.dispose();
                    disposableSubscriber = null;
                }

            }
        };

        Flowable.interval(0, 1, TimeUnit.SECONDS, Schedulers.newThread())
                .take(60)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(disposableSubscriber);
    }


    public void onPayPwdSettingSuccess() {
        userViewModel.update();
        finish();
    }
}
